import { app, BrowserWindow } from 'electron';
import { AppWindow, LoginWindow } from './window';
import path from 'path';

/**
 * 全局控制器
 *
 * @author chitanda
 * @date 2022-06-09 14:06:06
 * @export
 * @class GlobalController
 */
export class GlobalController {
  mainWindow!: AppWindow;
  loginWindow: LoginWindow | null = null;

  protected allWindows: BrowserWindow[] = [];

  /**
   * 当前激活窗口
   *
   * @author chitanda
   * @date 2022-06-09 15:06:42
   * @readonly
   * @type {(BrowserWindow | null)}
   */
  get activeWindow(): BrowserWindow | null {
    return BrowserWindow.getFocusedWindow();
  }

  /**
   * 打开主窗口
   *
   * @author chitanda
   * @date 2022-06-09 15:06:37
   * @return {*}  {Promise<AppWindow>}
   */
  async openMainWindow(): Promise<AppWindow> {
    this.mainWindow = new AppWindow({
      width: 1920,
      height: 1080,
    });
    if (app.isPackaged) {
      await this.mainWindow.openFile(path.join(__dirname, '../index.html'));
    } else {
      const { env } = process;
      await this.mainWindow.openUrl(`http://${env.VITE_DEV_SERVER_HOST}:${env.VITE_DEV_SERVER_PORT}`);
    }
    return this.mainWindow;
  }

  /**
   * 打开主窗口
   *
   * @author chitanda
   * @date 2022-06-09 15:06:37
   * @return {*}  {Promise<AppWindow>}
   */
  async openLoginWindow(): Promise<LoginWindow> {
    if (this.loginWindow) {
      return this.loginWindow;
    }
    const win = new LoginWindow(this.activeWindow!);
    if (app.isPackaged) {
      await win.openFile(path.join(__dirname, '../index.html'), { hash: '/login' });
    } else {
      const { env } = process;
      await win.openUrl(`http://${env.VITE_DEV_SERVER_HOST}:${env.VITE_DEV_SERVER_PORT}#/login`);
    }
    // 关闭所有其他窗口
    {
      this.mainWindow.close();
      this.allWindows.forEach(win => win.close());
      this.allWindows.forEach(win => win.destroy());
      this.allWindows = [];
    }
    this.loginWindow = win;
    return win;
  }

  /**
   * 关闭登录窗口
   *
   * @author chitanda
   * @date 2022-06-09 17:06:35
   */
  closeLoginWindow(): void {
    if (this.loginWindow) {
      this.openMainWindow();
      this.loginWindow.close();
      this.loginWindow.destroy();
      this.loginWindow = null;
    }
  }

  /**
   * 打开 MosDynamic 应用
   *
   * @author chitanda
   * @date 2022-06-09 15:06:13
   * @param {string} psDevSlnSys
   * @param {boolean} [manage=false] 是否为管理端模式
   * @return {*}  {Promise<AppWindow>}
   */
  async openMosDynamicWindow(psDevSlnSys: string, manage = false): Promise<AppWindow> {
    const mosDynamicWin = new AppWindow({
      width: 1920,
      height: 1080,
    });
    if (manage) {
      await mosDynamicWin.openFile(path.join(__dirname, '../../resources/MosDynamic/index.html'), { hash: `/mosdynamicmgr/${psDevSlnSys}` });
    } else {
      await mosDynamicWin.openFile(path.join(__dirname, '../../resources/MosDynamic/mosdynamicdesign.html'), { hash: `/mosdynamicdesign/${psDevSlnSys}/pssystems/views/tabexpview_tooltree` });
    }
    this.allWindows.push(mosDynamicWin);
    return mosDynamicWin;
  }

  /**
   * 当前激活窗口最小化
   *
   * @author chitanda
   * @date 2022-06-09 15:06:11
   */
  min(): void {
    const win = this.activeWindow;
    if (win) {
      win.minimize();
    }
  }

  /**
   * 当前激活窗口最大化
   *
   * @author chitanda
   * @date 2022-06-09 15:06:41
   */
  max(): void {
    const win = this.activeWindow;
    if (win) {
      if (win.isMaximized()) {
        win.unmaximize();
      } else {
        win.maximize();
      }
    }
  }

  /**
   * 关闭当前激活窗口
   *
   * @author chitanda
   * @date 2022-06-09 15:06:09
   */
  close(): void {
    const win = this.activeWindow;
    if (win) {
      win.close();
    }
  }
}
// 全局唯一控制器
export const ctl = new GlobalController();
